package com.ccmusic.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ccmusic.Message.Msg;
import com.ccmusic.entity.Matching;
import com.ccmusic.entity.User;
import com.ccmusic.service.MatchingService;
import com.ccmusic.service.UserService;
import com.ccmusic.service.impl.MatchingServiceImpl;
import com.ccmusic.service.impl.UserServiceImpl;
import com.google.gson.Gson;

/**
 * Servlet implementation class QtLoginServlet
 */
@WebServlet({ "/qtlogin", "/qtls.do" })
public class QtLoginServlet extends HttpServlet {
	UserService us = new UserServiceImpl();
	MatchingService matchingService=new MatchingServiceImpl();
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String op = "query";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
			//System.out.println(op);
		}
		
		if (op.equals("check")) {
			// 输入账号的时候进行验证数据库是否存在此账号?
			docheck(request, response);
		}
		if (op.equals("login")) {
			// 登录校验
			dolgin(request, response);
		}
	}

	private void dolgin(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json");
		HttpSession session = request.getSession();
		Gson gson = new Gson();
		if(session.getAttribute("user")!=null) {
			session.removeAttribute("user");
		}
		
		String uname = request.getParameter("uname");
		
		//System.out.println(uname);
		
		String pwd = request.getParameter("pwd");
		User u = us.check(uname);		
		Msg msg1 = new Msg();
		String ms = "";
		PrintWriter out = response.getWriter();
		
		if (u != null) {
			if (u.getPwd().equals(pwd)) {			
					session.setAttribute("user", u);
					request.getRequestDispatcher("CCMusicSys/homePage.jsp").forward(request, response);
			} else {

				ms = "密码错误";
			}
		} else {
			ms = "请输入正确的账号密码?";
			
		}
		msg1.setMsg(ms);
		
		String str = gson.toJson(msg1);
		//System.out.println(str);
		out.print(str);
		out.close();
	}

	public void docheck(HttpServletRequest request, HttpServletResponse response) throws IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json");
		String uname = request.getParameter("uname");
		User u = us.check(uname);
		Msg msg1 = new Msg();
		String ms = "";
		if (u == null) {
			ms = "该用户不存在";
		}
		msg1.setMsg(ms);
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		String str = gson.toJson(msg1);
		out.print(str);
		out.close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
